package com.app.tool;



import com.jiangdg.usbcamera.utils.Lf_Debug;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

public class RootCmd {
         private static final String TAG = "RootCmd";
         private static boolean mHaveRoot = false;
         // 判断机器Android是否已经root，即是否获取root权限
         public static boolean haveRoot() {
             if (!mHaveRoot) {
                 int ret = execRootCmdSilent("echo test"); // 通过执行测试命令来检测
                 if (ret != -1) {
                    Lf_Debug.dbg_mPrintf(Lf_Debug._FILE_LINE_FUNC_(), "have root!");
                    mHaveRoot = true;
                 } else {
                    Lf_Debug.dbg_mPrintf(Lf_Debug._FILE_LINE_FUNC_(), "not root!");
                 }
             } else {
                    Lf_Debug.dbg_mPrintf(Lf_Debug._FILE_LINE_FUNC_(), "mHaveRoot = true, have root!");
             }
             return mHaveRoot;
         }

         public static String execRootCmd(String cmd) {
             String result = "";
             DataOutputStream dos = null;
             DataInputStream dis = null;

         try {
             Process p = Runtime.getRuntime().exec("su");// 经过Root处理的android系统即有su命令
             dos= new DataOutputStream(p.getOutputStream());
             dis = new DataInputStream(p.getInputStream());

             Lf_Debug.dbg_mPrintf(Lf_Debug._FILE_LINE_FUNC_(), cmd);
             dos.writeBytes(cmd + "\n");
             dos.flush();
             dos.writeBytes("exit\n");
             dos.flush();
             String line = null;
             while ((line = dis.readLine()) != null)
             {
                 Lf_Debug.dbg_mPrintf(Lf_Debug._FILE_LINE_FUNC_(), line);
                result += line;
             }
             p.waitFor();
         }
         catch (Exception e)
         {
            e.printStackTrace();
         }
         finally {
                 if (dos != null)
                 {
                     try {
                        dos.close();
                     }
                     catch (IOException e)
                     {
                        e.printStackTrace();
                     }
                 }
                if (dis != null)
                {
                     try {
                        dis.close();
                     }
                     catch (IOException e)
                     {
                     e.printStackTrace();
                     }
                }
         }
         return result;
    }
     public static int execRootCmdSilent(String cmd) {
         int result = -1;
         DataOutputStream dos = null;

         try {
             Process p = Runtime.getRuntime().exec("su");
             dos = new DataOutputStream(p.getOutputStream());

             Lf_Debug.dbg_mPrintf(Lf_Debug._FILE_LINE_FUNC_(), cmd);
             dos.writeBytes(cmd + "\n");
             dos.flush();
             dos.writeBytes("exit\n");
             dos.flush();
             p.waitFor();
             result = p.exitValue();
         }
         catch (Exception e)
         {
            e.printStackTrace();
         }
         finally
         {
             if (dos != null) {
                 try {
                 dos.close();
                 } catch (IOException e) {
                 e.printStackTrace();
                 }
            }
         }
         return result;
     }
}
